﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace OptiGear1
{
    public class WowGem
    {
        public string name;
        public int wowheadid;
        public int spellpower;
        public int hit;
        public int haste;
        public int crit;
        public int spirit;
        public int intel;
        public int stamina;
        public int red;
        public int blue;
        public int yellow;
        public double weight;

        public string Name
        {
            get
            {
                return name;
            }
        }

        public WowGem()
        {
        }

        public WowGem(XmlNode node)
        {
        }

        public WowGem(int wowheadID)
        {
            string itemurl = "http://www.wowhead.com/?item=" + wowheadID + "&xml";
            string itemXml = Utils.GetHtml(itemurl);

            XmlDoc itemDoc = new XmlDoc();
            itemDoc.LoadXml(itemXml);

            this.wowheadid = wowheadID;
            name = itemDoc.GetNodeValue("//name").Replace("'", "");
            hit = 0;
            spellpower = 0;
            haste = 0;
            crit = 0;
            spirit = 0;
            intel = 0;
            stamina = 0;
            weight = 0.0;

            string dataValString = itemDoc.GetNodeValue("//jsonEquip");
            string[] dataVals = dataValString.Split(new char[] { ',' });
            for (int j = 0; j < dataVals.Length; j++)
            {
                string[] keyValuePair = dataVals[j].Split(new char[] { ':' });
                switch (keyValuePair[0])
                {
                    case ("\"critstrkrtng\""):
                        {
                            crit = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"hitrtng\""):
                        {
                            hit = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"splpwr\""):
                        {
                            spellpower = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"int\""):
                        {
                            intel = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"spi\""):
                        {
                            spirit = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"sta\""):
                        {
                            stamina = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    case ("\"hastertng\""):
                        {
                            haste = Int32.Parse(keyValuePair[1]);
                            break;
                        }
                    default:
                        {
                            break;
                        }
                }
            }

            //now get socket bonus and sockets
            string toolTipString = itemDoc.GetNodeValue("//htmlTooltip");
            red = toolTipString.Select((c, k) => toolTipString.Substring(k)).Count(sub => sub.StartsWith("Red"));
            blue = toolTipString.Select((c, k) => toolTipString.Substring(k)).Count(sub => sub.StartsWith("Blue"));
            yellow = toolTipString.Select((c, k) => toolTipString.Substring(k)).Count(sub => sub.StartsWith("Yellow"));
        }

        public static List<WowGem> Deserialize(string fileName)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(List<WowGem>));
            FileStream myFileStream = new FileStream(fileName, FileMode.Open);
            List<WowGem> gems = (List<WowGem>)mySerializer.Deserialize(myFileStream);
            myFileStream.Close();
            return gems;
        }

        public static void Serialize(List<WowGem> gems, string fileName)
        {
            XmlSerializer s = new XmlSerializer(typeof(List<WowGem>));
            TextWriter wr = new StreamWriter(fileName);
            s.Serialize(wr, gems);
            wr.Close();
        }
    }

    class WowGemComparer : IComparer<WowGem>
    {
        public int Compare(WowGem item1, WowGem item2)
        {
            if (item1.hit == item2.hit)
                return item2.weight.CompareTo(item1.weight);
            else
                return item2.hit.CompareTo(item1.hit);
             
        }
    }
}
